package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.suffix;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.ExtMyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.SuffixUtil;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ExtInsertSelectiveElementGenerator;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.InsertSelectiveElementGenerator;

/*********************************************************
 * 文件名称：SuffixInsertSelectiveElementGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.suffix
 * 功能说明：带后缀的条件插入生成器
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/29 9:50
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class SuffixInsertSelectiveElementGenerator extends ExtInsertSelectiveElementGenerator {
    @Override
    public void addElements(XmlElement parentElement) {
        XmlElement answer = buildInitialInsert(SuffixUtil.getSuffixMethodName(introspectedTable.getInsertSelectiveStatementId(),introspectedTable), new FullyQualifiedJavaType("map"));

        StringBuilder sb = new StringBuilder();

        sb.append("insert into "); //$NON-NLS-1$
        sb.append(SuffixUtil.addTableNameSuffix(introspectedTable.getFullyQualifiedTableNameAtRuntime(),introspectedTable));
        answer.addElement(new TextElement(sb.toString()));

        XmlElement insertTrimElement = new XmlElement("trim"); //$NON-NLS-1$
        insertTrimElement.addAttribute(new Attribute("prefix", "(")); //$NON-NLS-1$ //$NON-NLS-2$
        insertTrimElement.addAttribute(new Attribute("suffix", ")")); //$NON-NLS-1$ //$NON-NLS-2$
        insertTrimElement.addAttribute(new Attribute("suffixOverrides", ",")); //$NON-NLS-1$ //$NON-NLS-2$
        answer.addElement(insertTrimElement);

        XmlElement valuesTrimElement = new XmlElement("trim"); //$NON-NLS-1$
        valuesTrimElement.addAttribute(new Attribute("prefix", "values (")); //$NON-NLS-1$ //$NON-NLS-2$
        valuesTrimElement.addAttribute(new Attribute("suffix", ")")); //$NON-NLS-1$ //$NON-NLS-2$
        valuesTrimElement.addAttribute(new Attribute("suffixOverrides", ",")); //$NON-NLS-1$ //$NON-NLS-2$
        answer.addElement(valuesTrimElement);

        for (IntrospectedColumn introspectedColumn :
                ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {

            if (introspectedColumn.isSequenceColumn()
                    || introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
                // if it is a sequence column, it is not optional
                // This is required for MyBatis3 because MyBatis3 parses
                // and calculates the SQL before executing the selectKey

                // if it is primitive, we cannot do a null check
                sb.setLength(0);
                sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
                sb.append(',');
                insertTrimElement.addElement(new TextElement(sb.toString()));

                sb.setLength(0);
                sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn,"row."));
                sb.append(',');
                valuesTrimElement.addElement(new TextElement(sb.toString()));

                continue;
            }
            if (ExtMyBatis3FormattingUtilities.insertSysdate(introspectedColumn)) {
                sb.setLength(0);
                sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
                sb.append(',');
                insertTrimElement.addElement(new TextElement(sb.toString()));
            }else{
                sb.setLength(0);
                sb.append("row." + introspectedColumn.getJavaProperty());
                sb.append(" != null"); //$NON-NLS-1$
                XmlElement insertNotNullElement = new XmlElement("if"); //$NON-NLS-1$
                insertNotNullElement.addAttribute(new Attribute(
                        "test", sb.toString())); //$NON-NLS-1$

                sb.setLength(0);
                sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
                sb.append(',');
                insertNotNullElement.addElement(new TextElement(sb.toString()));
                insertTrimElement.addElement(insertNotNullElement);
            }

            if (ExtMyBatis3FormattingUtilities.insertSysdate(introspectedColumn)) {
                sb.setLength(0);
                sb.append("${getDBDate}");
                sb.append(',');
                valuesTrimElement.addElement(new TextElement(sb.toString()));
            } else {
                sb.setLength(0);
                sb.append("row." + introspectedColumn.getJavaProperty());
                sb.append(" != null"); //$NON-NLS-1$
                XmlElement valuesNotNullElement = new XmlElement("if"); //$NON-NLS-1$
                valuesNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$

                sb.setLength(0);
                sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn,"row."));
                sb.append(',');
                valuesNotNullElement.addElement(new TextElement(sb.toString()));
                valuesTrimElement.addElement(valuesNotNullElement);
            }
        }

        if (context.getPlugins().sqlMapInsertSelectiveElementGenerated(answer, introspectedTable)
                && SuffixUtil.isEnableSuffix(introspectedTable)) {
            SuffixUtil.deleteOldElement(parentElement,answer);
            parentElement.addElement(answer);
        }
    }
}
